import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    map: {}
  },
  getters: {
    //获取值
    get: state => (key, defaultValue) => {
      console.log(22222)
      let value = state.map[key]
      return value ? value : defaultValue
    }
  },
  mutations: {
    //存值
    put: (state, payload) => {
      console.log(111111)
      state.map[payload.k] = payload.v
    },
    clear: (state, key) => {
      delete state[key]
    }
  },
  actions: {
    put: (content, payload) => {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          content.commit('put', payload)
          resolve()
        }, 200)
      })
    }, clear: (content, key) => {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          content.commit('clear', key)
          resolve()
        }, 200)
      })
    }
  }
})

export default store
